/*
 * @Author: shanghongyun 2879455630@qq.com
 * @Date: 2025-06-28 14:25:40
 * @LastEditors: shanghongyun 2879455630@qq.com
 * @LastEditTime: 2025-07-09 08:58:07
 * @FilePath: \A06\Device\dev.c
 * @Description: 外设管理
 */

#include "dev.h"
#include "systick.h"
#include "task.h"


static void PVD_config(void){
    RCC_Unlock();
    RCC_APBPeriphClockCmd(RCC_APB_PERIPH_PVD, ENABLE);
    RCC_Lock();

    PVD_InitTypeStruct PVD_InitStruct;
    /* Config PVD */
    PVD_StructInit(&PVD_InitStruct);
    PVD_InitStruct.PVD_TriggerType = PVD_TRIGGER_TYPE_HIGH_LEVEL;
    PVD_InitStruct.PVD_TriggerResult = PVD_TRIGGER_RESULT_INT;
    PVD_InitStruct.PVD_VrefSel = PVD_VREF_SEL_26;//PVD_VREF_SEL_42; /* 设置低于2.6V进入PVD中断 */
    PVD_InitStruct.PVD_Filter.FiltSample = PVD_FILTER_SAMPLE_PCLK32;
    PVD_InitStruct.PVD_Filter.FiltLen = PVD_FILTER_LEN32;
    PVD_InitStruct.PVD_Filter.FiltState = ENABLE;
    PVD_Init(&PVD_InitStruct);
    /* Enable PVD */
    PVD_Cmd(ENABLE);
    /* Enable PVD Interrupt */
    NVIC_SetPriority(PVD_IRQn, 2);
    NVIC_EnableIRQ(PVD_IRQn);
    PVD_IntCmd(ENABLE);
}


/**
 *******************************************************************************
 * @brief   System Clock initialization.
 * @param   None.
 * @retval  None.
 ******************************************************************************/
void SystemClockInit(void)
{
    RCC_ClkInitTypeStruct RCC_ClkInitStruct;

    RCC_Unlock();

    RCC_ClockStructInit(&RCC_ClkInitStruct);
    RCC_ClkInitStruct.RCC_ClockType = (uint32_t)(RCC_CLOCK_TYPE_HCLK | RCC_CLOCK_TYPE_PCLK);

    RCC_ClkInitStruct.HCLKDivider = RCC_SYSCLK_DIV1;
    RCC_ClkInitStruct.PCLKDivider = RCC_HCLK_DIV1;
    RCC_ClockConfig(&RCC_ClkInitStruct);
    /*开启外设时钟*/
    RCC_AHBPeriphClockCmd(RCC_AHB_PERIPH_GPIO, ENABLE);
    RCC_APBPeriphClockCmd(RCC_APB_PERIPH_ADC, ENABLE);
    RCC_APBPeriphClockCmd(RCC_APB_PERIPH_EPWM, ENABLE);
    RCC_APBPeriphClockCmd(RCC_APB_PERIPH_PVD, ENABLE);

    RCC_Lock();
}


/**
 * @description: 外设初始化
 * @return {*}
 */
void dev_init(void){
    /*时钟初始化*/
    SystemClockInit();

    /*PVD初始化*/
    PVD_config();
    
    /*systick初始化*/
    Systick_Init();

    /* GPIO初始化 */
    gpio_config();

    /*PGA初始化*/
    PGA_config();

    /* ADC初始化 */
    adc_config();

    /* EPWM初始化 */
    epwm_config();
    
    /* 初始化任务 */
    Task_init();

    /* 初始化RTT */
    SEGGER_RTT_Init();
}


